package cc.minos.servers.openfire.managers
{
	import cc.minos.servers.openfire.model.ConfigParameters;
	import org.igniterealtime.xiff.core.UnescapedJID;
	import org.igniterealtime.xiff.data.im.RosterItemVO;
	import org.igniterealtime.xiff.data.Message;
	
	/**
	 * ...
	 * @author Minos
	 */
	public class OpenFireManager
	{
		
		private static var _connectionManager:ConnectionManager;
		private static var _rosterManager:RosterManager;
		private static var _mucManager:MUCManager;
		private static var _chatManager:ChatManager;
		private static var _messageManager:MessageManager;
		private static var _historyManager:HistoryManager;
		
		private static var _red5Manager:Red5Manager;
		private static var _iqManager:IQManager;
		
		protected static var _errorHandler:Function;
		protected static var _configProvider:Function;
		
		//
		public static var config:ConfigParameters;
		
		private static var _me:RosterItemVO;
		public function OpenFireManager()
		{
		}
		
		/* connection manager  */
		
		public static function get connectionManager():ConnectionManager
		{
			if ( !_connectionManager )
				_connectionManager = new ConnectionManager( config.connectionType , config.port );
			return _connectionManager;
		}
		
		public static function login( username:String , password:String ):void
		{
			connectionManager.connection.useAnonymousLogin = config.anonymous;
			connectionManager.login( username , password , config.domain , config.server );
		}
		
		public static function logout():void
		{
			connectionManager.logout();
		}
		
		/* */
		
		public static function get rosterManager():RosterManager
		{
			if ( !_rosterManager )
				_rosterManager = new RosterManager( connectionManager.connection )
			return _rosterManager;
		}
		
		public static function get mucManager():MUCManager
		{
			if ( !_mucManager )
				_mucManager = new MUCManager( connectionManager.connection )
			return _mucManager;
		}
		
		public static function get chatManager():ChatManager
		{
			if ( !_chatManager )
				_chatManager = new ChatManager();
			return _chatManager;
		}
		
		public static function get messageManager():MessageManager
		{
			if ( !_messageManager )
				_messageManager = new MessageManager( connectionManager.connection );
			return _messageManager;
		}
		
		/* */
		
		public static function get historyManager():HistoryManager
		{
			if ( !_historyManager )
				_historyManager = new HistoryManager();
			return _historyManager;
		}
		
		/* */
		
		public static function get red5Manager():Red5Manager
		{
			if ( !_red5Manager )
				_red5Manager = new Red5Manager();
			return _red5Manager;
		}
		
		/* */
		
		public static function get iqManager():IQManager
		{
			if ( !_iqManager )
				_iqManager = new IQManager( connectionManager.connection );
			return _iqManager;
		}
		
		
		
		public static function displayError( name:String , message:String , fatal:Boolean = false ):void
		{
			errorHandler( name , message , fatal );
		}
		
		public static function get errorHandler():Function
		{
			return _errorHandler;
		}
		
		public static function set errorHandler( handler:Function ):void
		{
			_errorHandler = handler;
		}
		
		static public function get me():RosterItemVO
		{
			return _me;
		}
		
		static public function set me( value:RosterItemVO ):void
		{
			_me = value;
		}
		
		public static function clearAllManager():void
		{
			_connectionManager = null;
			_mucManager = null;
			_rosterManager = null;
			_chatManager = null;
			_messageManager = null;
			_historyManager = null;
		}
		
		public static function getRoomsByServic( s:String ):void
		{
			var roomServic:UnescapedJID = new UnescapedJID( s + "." + connectionManager.connection.domain );
			mucManager.getConferenceItems( roomServic );
		}
	
	}

}